from rest_framework.viewsets import ModelViewSet
from rest_framework.generics import GenericAPIView
from rest_framework.views import APIView
from apps.recruit.serializers import*
from rest_framework.response import Response

#热门城市的显示
class HotCityModelViewSet(ModelViewSet):

    permission_classes = []
    queryset = City.objects.all()
    serializer_class = HotCityModelSerializer

#增加公司访问次数
class EnterpriseVisit(GenericAPIView):

    # permission_classes = []
    queryset = Enterprise.objects.all()

    serializer_class = EnterpriseVisitSerializer

    def put(self,request,pk):
        enterprise = self.get_object()
        enterprise.visits += 1
        enterprise.save()
        return Response({'messsage':'更新成功','sucess':'true'})

#增加职业访问次数
class RecruitVisit(GenericAPIView):

    # permission_classes = []
    queryset = Recruit.objects.all()

    serializer_class = RecruitVisitSerializer

    def put(self,request,pk):
        recruit = self.get_object()
        recruit.visits += 1
        recruit.save()
        return Response({'messsage':'更新成功','sucess':'true'})



# 公司的显示
class EnterpriseModelViewSet(ModelViewSet):

    permission_classes = []
    queryset = Enterprise.objects.all()
    serializer_class = EnterpriseModelSerializer

    # @cache_response(timeout=60 * 60, cache='default')
    def list(self, request, *args, **kwargs):
        enterprises = self.get_queryset()
        ser = super().get_serializer(enterprises, many=True)
        for enterprise in ser.data:
            # recruit = enterprise.recruits.count()
            # 获得职位名称
            enterprise_id = enterprise.get('id')
            recruits = Recruit.objects.filter(enterprise_id=enterprise_id)
            recruit_list = []
            for recruit in recruits:
                recruit_list.append(recruit.jobname)
            # 增加额外字段
            enterprise['recruits'] = recruit_list

        return Response(ser.data)


#职业的显示
class RecruitModelViewSet(ModelViewSet):

    permission_classes = []
    queryset = Recruit.objects.all()
    serializer_class = RecruitModelSerializer

#职业详情
class RecruitDetailsModelViewSet(ModelViewSet):

    permission_classes = []
    queryset = Recruit.objects.all()
    serializer_class = RecruitDetailsListSerializer

#公司详情
class EnterpriseDetailsModdelViewSet(ModelViewSet):

    permission_classes = []
    queryset = Enterprise.objects.all().order_by('?')
    serializer_class = EnterpriseRecruitModelSerializer

#收藏公司
class EnterpriseCollectAPIView(APIView):

    # permission_classes = [IsAuthenticated]
    permission_classes = []

    def post(self, request, pk):
        enterprise = Enterprise.objects.get(pk=pk)
        user = self.request.user
        enterprise.users.add(user)

        return Response({"message": "收藏成功", "success": "true"})
#取消收藏公司
class EnterpriseUnCollectAPIView(APIView):
    permission_classes = []

    def post(self, request, pk):
        enterprise = Enterprise.objects.get(pk=pk)
        user = self.request.user
        enterprise.users.remove(user)

        return Response({"message": "取消收藏成功", "success": "true"})

# 收藏职位
class RecruitCollectAPIView(APIView):
    permission_classes = []

    def post(self, request, pk):
        recruit = Recruit.objects.get(pk=pk)
        user = self.request.user
        recruit.users.add(user)

        return Response({"message": "收藏成功", "success": "true"})


# 取消收藏职位
class RecruitUnCollectAPIView(APIView):

    permission_classes = []

    def post(self, request, pk):
        recruit = Recruit.objects.get(pk=pk)
        user = self.request.user
        recruit.users.remove(user)

        return Response({"message": "取消收藏成功", "success": "true"})

#搜索
class SearchRecruitAPIView(APIView):
    def post(self,request):
        data = request.data
        city = data.get('cityname')
        keyword = data.get('keyword')
        if city is None:
            recruits = Recruit.objects.filter(jobname__contains=keyword)
        else:
            recruits = Recruit.objects.filter(city=city,jobname__contains=keyword)
        recruit_list = []
        for recruit in recruits:
            serializer = RecruitDetailsModelSerializer(recruit)
            recruit_list.append(serializer.data)
        return Response(recruit_list)
